﻿using System.Collections.Generic;
using System.Linq;
using NetworkCommsDotNet;
using DPSBase;
using System.Windows.Media;
using System.Windows;
using System.Collections.ObjectModel;
using JiveMessenger.Models;
using JiveMessenger.ViewModels;
using JiveMessenger.Helpers;
using JiveMessenger.Network;
using JmShared;
using JiveMessenger.Settings;
using System;

namespace JiveMessenger.User
{
    public class Client
    {
        private Dictionary<string, string> _defaultEmoticonDic;
        public static readonly UserInfo GreenText = new UserInfo("GreenText", "1") { Color = "#FF008000", FontStyle = FontStyles.Normal.ToString(), FontFamily = "Segoe UI" };
        public static readonly UserInfo RedText = new UserInfo("RedText", "2") { Color = "#800000", FontStyle = FontStyles.Normal.ToString(), FontFamily = "Segoe UI" };
        public static readonly UserInfo GrayText = new UserInfo("GrayText", "3") { Color = "#FFA9A9A9", FontStyle = FontStyles.Normal.ToString(), FontFamily = "Segoe UI" };
        public WpUserSettings UserSettings { get; set; }
        public Dictionary<string, UserInfo> ConnectedClients { get; set; }//key:UserInfo.UserID
        public UserInfo CurrentUserInfo { get; set; }
        public Dictionary<string, List<ChatMessage>> PrivateMessages { get; set; }//key:UserInfo.UserName
        //public string OpenPmUserName { get; set; }
        public bool IsLoggedIn { get; set; }
        public bool IsAfk { get; set; }
        public bool IsBusy { get; set; }
        public string LastConnectedUserName { get; set; }

        private static Client _instance;
        public static Client Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new Client();
                    return _instance;
                }
                else
                    return _instance;
                
            }
        }

        private Client()
        {
            ConnectedClients = new Dictionary<string, UserInfo>();
            PrivateMessages = new Dictionary<string, List<ChatMessage>>();
        }
        public Dictionary<string, string> GetEmoticons(string userID, bool includeDefault, bool includeCustom, bool includeLineAndSpacer)
        {
            if (this.CurrentUserInfo == null)
                return new Dictionary<string, string>();
            if (userID == this.CurrentUserInfo.UserID)
                return GetEmoticons(this.CurrentUserInfo, includeDefault, includeCustom, includeLineAndSpacer);
            else
            {
                UserInfo user;
                if (this.ConnectedClients.TryGetValue(userID, out user))
                {
                    return GetEmoticons(user, includeDefault, includeCustom, includeLineAndSpacer);
                }
                else
                    return new Dictionary<string, string>();
            }
        }
        public bool ValidateEmoticonShortcut(string str, out string error)
        {
            return ValidateEmoticonShortcut(str, string.Empty, out error);
        }
        public bool ValidateEmoticonShortcut(string str, string exludeShortcut, out string error)
        {
            error = string.Empty;
            List<string> keys = GetEmoticons(CurrentUserInfo, true, true, false).Keys.ToList();
            if (!String.IsNullOrEmpty(exludeShortcut))
            {
                if (keys.Contains(exludeShortcut))
                    keys.Remove(exludeShortcut);
            }
            if (str.Length == 1)
            {
                error = "Minimum 2 characters";
                return false;
            }
            if (str.Length > 10)
            {
                error = "Max 10 characters";
                return false;
            }

            if (keys.Any(x => x.Contains(str)))
            {
                error = "Shortcut already in use or conflicted";
                return false;
            }
            return true;
        }

        public Dictionary<string, string> GetEmoticons(UserInfo user, bool includeDefault, bool includeCustom, bool includeLineAndSpacer)
        {
            Dictionary<string, string> allEmoticons;
            if (includeLineAndSpacer)
                allEmoticons = new Dictionary<string, string> { { "<line>", "line.gif" }, { "<spacer>", "spacer.gif" } };
            else
                allEmoticons = new Dictionary<string, string>();
            if (user == null)
                return allEmoticons;

            if (includeCustom)
            {
                if (user.CustomEmoticons != null)
                {
                    foreach (KeyValuePair<string, string> kvp in user.CustomEmoticons)
                    {
                        string folder = user.Equals(this.CurrentUserInfo) ? StaticSettings.CustomEmoticonFolder : StaticSettings.CustomContactEmoticonFolder;
                        string filePath = StaticMethods.CombinePath(folder, user.UserName, kvp.Value);
                        allEmoticons.Add(kvp.Key, filePath);
                    }
                }
            }
            if (includeDefault)
            {
                if (_defaultEmoticonDic == null)
                {
                    _defaultEmoticonDic = new Dictionary<string, string>();
                    foreach (KeyValuePair<string[], string> kvp in StaticSettings.DefaultEmoticons)
                    {
                        foreach (string s in kvp.Key)
                        {
                            string key = s.ToLower();
                            if (!_defaultEmoticonDic.ContainsKey(key))
                                _defaultEmoticonDic.Add(key, kvp.Value);
                            if (!_defaultEmoticonDic.ContainsKey(s))
                                _defaultEmoticonDic.Add(s, kvp.Value);
                        }
                    }
                }
                allEmoticons.AddRange(_defaultEmoticonDic);
            }
            return allEmoticons;
        }
        public void SetUserSettings(WpUserSettings settings)
        {
            this.UserSettings = settings;
            if (UserSettings.CustomEmoticons == null)
                UserSettings.CustomEmoticons = new Dictionary<string, string>();
            if (UserSettings.IgnoredContacts == null)
                UserSettings.IgnoredContacts = new List<string>();
            //if (UserSettings.OpenGroupChats == null)
            //    UserSettings.OpenGroupChats = new List<string>();
            if (UserSettings.BlockedGroups == null)
                UserSettings.BlockedGroups = new List<string>();
        }
        public static void Clear()
        {
            _instance = null;
        }
    }
}
